/**
* @version 1.0
* @package FACTURAPYMES.
* @author Jorge Washington Mueses Cevallos.
* @copyright Copyright (C) 2010 por Jorge Mueses. Todos los derechos reservados.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL.
* FACTURAPYMES! es un software de libre distribución, que puede ser
* copiado y distribuido bajo los términos de la Licencia Pública
* General GNU, de acuerdo con la publicada por la Free Software
* Foundation, versión 2 de la licencia o cualquier versión posterior.
*/

package jm.fac;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import jm.seg.clas.Rol;
import jm.fac.clas.Cliente;
import java.sql.ResultSet;
import jm.web.Fecha;

/**
 *
 * @author Jorge
 */
public class frmCambioCliente extends HttpServlet {
    private String _ip = null;
    private int _puerto = 5432;
    private String _db = null;

    public void init(ServletConfig config) throws ServletException
    {
        this._ip = config.getServletContext().getInitParameter("_IP");
        this._puerto = Integer.parseInt(config.getServletContext().getInitParameter("_PUERTO"));
        this._db = config.getServletContext().getInitParameter("_DB");
    }
    /**
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession sesion = request.getSession(true);
        String id_rol = (String)sesion.getAttribute("id_rol");
        String usuario = (String)sesion.getAttribute("usuario");
        String clave = (String)sesion.getAttribute("clave");

        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "Mon, 01 Jan 2001 00:00:01 GMT");
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Cache-Control", "must-revalidate");
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();

        Rol objRol = new Rol(this._ip, this._puerto, this._db, usuario, clave);


        try {
            String id = request.getParameter("id");
            String ruc = "";
            String razon_social = "";
            String direccion = "";
            String telefono = "";


            if(id.compareTo("-1")!=0){
                Cliente objCliente = new Cliente(this._ip, this._puerto, this._db, usuario, clave);
                try{
                    ResultSet rsCliente = objCliente.getCliente(id);
                    if(rsCliente.next()){
                        ruc = (rsCliente.getString("ruc")!=null) ? rsCliente.getString("ruc") : "";
                        razon_social = (rsCliente.getString("razon_social")!=null) ? rsCliente.getString("razon_social") : "";
                        direccion = (rsCliente.getString("direccion")!=null) ? rsCliente.getString("direccion") : "";
                        telefono = (rsCliente.getString("telefono")!=null) ? rsCliente.getString("telefono") : "";
                        rsCliente.close();
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    objCliente.cerrar();
                }
            }

            String html = "obj»cmp1_html^foc»caRuc^frm»";
            html += "<div class='H3' style='width:330px'>Formulario de Cambio de Cliente</div>";


            html += "<form action='frmCambioClienteGuardar' onsubmit='return fac_clienteCambioGuardar(this)' autocomplete='off'>";
            html += "<input type='hidden' id='idInsCli' name='idInsCli' value='"+id+"' />";
            html += "<input type='hidden' id='idIns' name='idIns' value='-1' />";
            html += "<input type='hidden' id='idCliNue' name='idCliNue' value='' />";
            html += "<table><tr><td>C&eacute;dula o RUC: </td>";
            html += "<td nowrap><input id='estab' name='estab' type='text' size='3' maxlength='3' value='001' onkeypress=\"_numero(event);\" />-<input id='caRuc' name='caRuc' type='text' size='25' maxlength='13' value='"+ruc+"' /> " +
                    "<input type='button' value='buscar' onkeypress=\"var c=(_esN)?event.which:event.keyCode;if(c==13){fac_getClienteCambio();}\" onclick='fac_getClienteCambio()' /> </td></tr>";
            html += "<tr><td>Fecha: </td>";
            html += "<td>"+Fecha.getFecha("SQL")+"</td></tr>";
            html += "<tr><td>Raz&oacute;n Social: </td>";
            html += "<td id='caRaZo'>"+razon_social+"</td></tr>";
            html += "<tr><td>Direcci&oacute;n: </td>";
            html += "<td id='caDir'>"+direccion+"</td></tr>";
            html += "<tr><td>Tel&eacute;fonos:</td>";
            html += "<td id='caTel'>"+telefono+"</td></tr>";
            
            html += "<tr><td colspan='2'><input type='submit' value='Registrar cambio' /></td></tr>";
            html += "</table>";    
            html += "</form>";
            

            out.print(html);

        } finally {
            objRol.cerrar();
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}